<template>
  <div v-loading="loading" class="list-table">
    <el-form ref="queryForm" :model="queryParams" :inline="true" @submit.native.prevent>
      <el-form-item :label="$t('指标年度')" prop="indicatorYear">
        <DatePicker v-model="queryParams.indicatorYear" type="year" value-format="yyyy" :placeholder="$t('指标年度')" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:indicators:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </el-col>
    </el-row>
    <el-table v-if="tableRender" :data="data.rows" border class="tableRender" style="width: 100%" :cell-style="getColorStyle">
      <el-table-column
        v-for="(item, index) in data.column"
        :key="index"
        :label="$t(item.label)"
        :prop="item.year + item.quarter"
        align="center"
        show-overflow-tooltip
        min-width="180"
        :fixed="item.year === 'indicatorName'"
      >
        <template v-if="item.year !== 'indicatorName'" slot="header">
          <span>{{ item.year + $t(item.label) }}</span>
          <el-tooltip placement="top">
            <div slot="content">
              <p>{{ $t('完成情况:') }}</p>
              <p>{{ $t('大于等于100 绿色') }}</p>
              <p>{{ $t('大于等于70 黄色') }}</p>
              <p>{{ $t('大于等于50 红色') }}</p>
              <p>{{ $t('小于50 黑色') }}</p>
            </div>
            <i class="el-icon-question" />
          </el-tooltip>
          <el-button
            v-hasPermi="['deal:indicators:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(item)"
          />
          <el-button
            v-hasPermi="['deal:indicators:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(item)"
          />
        </template>
        <template slot-scope="scope">
          <span v-if="item.year !== 'indicatorName'" :style="getColorStyle(scope.row)">{{ moneyFormat(scope.row[item.year + item.quarter]) }}</span>
          <span v-else>{{ scope.row[item.year + item.quarter] }}</span>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改运营指标监控对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <dealOperationalIndicatorsForm ref="dealOperationalIndicatorsForm" />
    </el-dialog>
  </div>
</template>

<script>
import { delDealOperationalIndicators, exportDealOperationalIndicators, getGroupByIndicatorName } from '@/api/deal/operational/indicators/dealOperationalIndicators'
import dealOperationalIndicatorsForm from './dealOperationalIndicatorsForm.vue'
export default {
    components: {
        dealOperationalIndicatorsForm
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 运营指标监控表格数据
          dealOperationalIndicatorsList: [],
          // 弹出层标题
          title: '运营指标',
          // 是否显示弹出层
          formOpen: false,
          tableRender: true,
          data: {},
        // 季度字典
        quarterOptions: [],
          // 查询参数
          queryParams: {
              companyId: this.$route.query.companyId,
              indicatorYear: undefined,
              quarter: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {}
      }
  },
  created() {
      this.getList()
  },
  methods: {
    /** 查询运营指标监控列表 */
    getList() {
        this.loading = true
      this.tableRender = false
      getGroupByIndicatorName(this.queryParams).then(res => {
        this.data = Object.assign({}, this.data, res.data)
        this.tableRender = true
        this.loading = false
      })
    },
    // 季度字典翻译
    quarterFormat(row) {
      this.getDicts('indicator_quarter').then(response => {
        this.quarterOptions = response.data
        return this.selectDictLabel(this.quarterOptions, row)
      })
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            companyId: undefined,
            indicatorYear: undefined,
            quarter: undefined,
            entrant: undefined,
            entryDate: undefined,
            remark: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.dealOperationalIndicatorsForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.dealOperationalIndicatorsForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delDealOperationalIndicators(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
        const queryParams = this.queryParams
        this.$confirm(this.$t('是否确认导出?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return exportDealOperationalIndicators(queryParams)
        }).then(response => {
            this.download(response.msg)
        }).catch(function() {
        })
    },
    getColorStyle({ row, column, columnIndex }) {
      if (columnIndex !== 0 && row && column) {
        if (row[column.property + 'CompleteStatus'] >= 100) {
          return 'color:#73d13d'
        } else if (row[column.property + 'CompleteStatus'] >= 70) {
          return 'color:#ffc53d'
        } else if (row[column.property + 'CompleteStatus'] >= 50) {
          return 'color:#EA7154'
        } else {
          return 'color:#272D30'
        }
      }
    }
  }
}
</script>
